package com.lvhx.controller;

import com.lvhx.service.PaymentService;
import com.lvhx.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "重试方式")
@RestController
@Slf4j
@RequestMapping("/retry")
public class RetryController {
    @Autowired
    private UserService userService;
    @ApiOperation("使用 Guava 的 Retryer")
    @RequestMapping(value = {"/guavaRetry"}, method = RequestMethod.GET)
    public void guavaRetry() {
        log.info("使用 Guava 的 Retryer");
        PaymentService service = new PaymentService();
        boolean result = service.payWithRetry("ORDER123456", 100.50);

        System.out.println("最终支付结果: " + (result ? "成功" : "失败"));


    }


    @ApiOperation("使用 spring 的 Retryer")
    @RequestMapping(value = {"/springRetry"}, method = RequestMethod.GET)
    @Transactional
    public String springRetry() {
        try {
            return userService.getUserInfo("1");
        } catch (Exception e) {
            return "{'error':'"+e.getMessage()+"'}";
        }
    }
}
